package com.wondersgroup.wuhan_demo.ui;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wondersgroup.wuhan_demo.R;
import com.wondersgroup.wuhan_demo.adapter.DoctorAdapter;
import com.wondersgroup.wuhan_demo.base.BaseFragment;
import com.wondersgroup.wuhan_demo.entity.DoctorVo;

import java.util.ArrayList;
import java.util.List;

public class DoctorSelectFragment extends BaseFragment {

    private RecyclerView recyclerView;
    private DoctorAdapter adapter;
    private List<DoctorVo> data = new ArrayList<>();
    private String deptName;

    public static DoctorSelectFragment newInstance(String deptName) {

        Bundle args = new Bundle();
        args.putString("dept", deptName);
        DoctorSelectFragment fragment = new DoctorSelectFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        deptName = getArguments() == null ? "" : getArguments().getString("dept");
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_doctor_select, container, false);
        recyclerView = view.findViewById(R.id.recyclerview);
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 4, LinearLayoutManager.VERTICAL, false));
        adapter = new DoctorAdapter(R.layout.item_doctor_select, data);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                Toast.makeText(getContext(), data.get(position)+"--clicked", Toast.LENGTH_SHORT).show();
                getActivity().getSupportFragmentManager().beginTransaction().hide(DoctorSelectFragment.this)
                        .add(R.id.content, YySureFragment.newInstance(deptName,
                                data.get(position).getDoctorName()
                        )).addToBackStack(null).commit();
            }
        });
        recyclerView.setAdapter(adapter);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        LaunchActivity launchActivity=(LaunchActivity)getActivity();
        launchActivity.setTopPosition(3);
        initDoctor();
    }

    private void initDoctor() {
        String[] xing = {"赵", "钱", "孙", "李", "周", "吴", "郑", "王"};
        String[] disease = {"心脏病", "高血压", "肺癌", "鼻咽癌", "白血病", "糖尿病", "肝炎", "痢疾", "结石专病", "风湿病"};
        data.clear();
        for (int i = 0; i < 10; i++) {
            int random = (int) (Math.random() * 10);
            data.add(new DoctorVo(xing[random % xing.length] + "医生", disease[random % disease.length] + "与" +
                    disease[(random + 1) % disease.length] + "的治疗"));
        }
        adapter.notifyDataSetChanged();
    }

}
